package com.itheima.controller;

import com.itheima.commons.enums.ResultEnum;
import com.itheima.commons.enums.TipsEnum;
import com.itheima.commons.pojo.CommonEntity;
import com.itheima.commons.result.ResponseData;
import com.itheima.service.ElasticsearchIndexService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: itheima-technology-platform
 * @description: 索引操作控制器
 * @author: zhanghz001
 * @create: 2021-05-17 16:49
 **/
// @RestController
@RequestMapping("/v1/indices")
@Slf4j
public class ZhzElasticsearchIndexController {
    @Autowired
    ElasticsearchIndexService elasticsearchIndexService;
    
    /**
     * 增加索引
     * @param commonEntity 
     * @return
     */
    @RequestMapping("/add")
    public ResponseData addIndex(CommonEntity commonEntity) {
        ResponseData responseData = new ResponseData();
        //如果需要添加的index为空
        if (StringUtils.isEmpty(commonEntity.getIndexName())) {
            return responseData;
        }
        try {
            //通过高阶API调用增加索引方法
            boolean b = elasticsearchIndexService.addIndexAndMapping(commonEntity);
            //构建返回信息,通过类型推断自动装箱(多个参数取交集)
            responseData.setResultEnum(b, ResultEnum.SUCCESS, 1);
            //日志记录
            log.info(TipsEnum.CREATE_DOC_SUCCESS.getMessage());
        } catch (Exception e) {
            //日志记录
            log.error(TipsEnum.CREATE_DOC_FAIL.getMessage(), e);
            //构建错误返回
            responseData.setResultEnum(ResultEnum.ERROR);
        } finally {
            
        }
        return responseData;
    }
}
